﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;

namespace MSSQL.DBSync.UI.Common.Converts
{
    /// <summary>
    /// 数据库连接状态转化器
    /// db connection state int <=> string
    /// 0 = black
    /// 1 = green
    /// 2 = red
    /// </summary>
    public class DBConnStateCvt : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || string.IsNullOrEmpty(value.ToString()))
            {
                return "Black";
            }

            try
            {
                var intvalue = (int)value;
                switch (intvalue)
                {
                    case 0:
                        return "Black";
                    case 1:
                        return "Green";
                    case 2:
                        return "Red";
                    default:
                        return "Black";
                }
            }
            catch (Exception e)
            {
                return "Black";
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return 0;
            }
            
            try
            {
                string strvalue = value.ToString();
                switch (strvalue)
                {
                    case "Black":
                        return 0;
                    case "Green":
                        return 1;
                    case "Red":
                        return 2;
                    default:
                        return 0;
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }
}
